package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.Classes;
import com.itheima.entity.PageBean;
import com.itheima.entity.PageQueryObject;
import com.itheima.mapper.ClassMapper;
import com.itheima.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    private ClassMapper classMapper;


    @Override
    public PageBean Serch(PageQueryObject pageRequestParam) {
        PageHelper.startPage(pageRequestParam.getPage(),pageRequestParam.getPageSize());
        Page page = (Page) classMapper.selectByParam(pageRequestParam);
        return new PageBean(page.getTotal(),page.getResult());
    }

    @Override
    public boolean isHasStudent(Integer id) {
        return classMapper.isHasStudent(id) > 0;
    }

    @Override
    public void delete(Integer id) {
        classMapper.delete(id);
    }


    @Override
    public void save(Classes clazz) {
        clazz.setCreateTime(LocalDate.now());
        clazz.setUpdateTime(LocalDate.now());
//        根据开始时间和结束时间与现在时间进行比较结果来设置状态已开课，已结课，未开课
        if (clazz.getBeginDate().isBefore(LocalDate.now()) && clazz.getEndDate().isAfter(LocalDate.now())) {
            clazz.setStatus("已开课");
        } else if (clazz.getBeginDate().isAfter(LocalDate.now())) {
            clazz.setStatus("未开课");
        } else if (clazz.getEndDate().isBefore(LocalDate.now())) {
            clazz.setStatus("已结课");
        }

        classMapper.save(clazz);
    }

    @Override
    public Classes getById(Integer id) {
        return classMapper.getById(id);
    }

    @Override
    public void update(Classes clazz) {
        classMapper.update(clazz);
    }

    @Override
    public List<Classes> list() {
        return classMapper.list();
    }
}
